export default async ({ params, request }) => {
  // 解析URL中的参数
  const url = new URL(request.url)
  // 获取查询参数searchWord
  const searchWord = url.searchParams.get('searchWord')

  const { categoryId } = params
  // 查找所有contents
  let sql = `select * from contents`

  // 查找包含searchWord的所有contents
  if (searchWord) {
    sql += ` where title like @searchWord order by id desc`
    return window.api.sql(sql, 'findAll', { searchWord: `%${searchWord}%` })
  }

  console.log('categoryId:', categoryId)

  // 查找指定categoryId的所有contents包括未分类(categoryId = 0)
  if (categoryId !== undefined) {
    sql += ` where category_id=${categoryId}`
  }
  // 按id降序排列所有contents
  sql += ` order by id desc`
  return window.api.sql(sql, 'findAll')
}
